package com.sztx.pay.center.core.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.util.Constance.ActLevel;
import com.sztx.pay.center.common.util.Constance.IdCardType;
import com.sztx.pay.center.common.util.Constance.UserType;
import com.sztx.pay.center.common.util.IDCardUtil;
import com.sztx.pay.center.common.util.StringUtil;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.UserInfoService;
import com.sztx.pay.center.dataaccess.dao.mysql.UserInfoMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.redis.UserRedisDAO;
import com.sztx.pay.center.dataaccess.domain.UserInfoDO;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.core.service.impl.BaseServiceImpl;


@Service
public class UserInfoServiceImpl extends BaseServiceImpl implements UserInfoService{
	@Autowired
	private UserInfoMysqlDAO userInfoMysqlDAO; 
	
	@Autowired
	private UserRedisDAO userRedisDAO;

	@Override
	public void createUser(UserInfoDO userInfo) {		
		String userId = userInfo.getUserId();
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		
		UserInfoDO uInfo = userInfoMysqlDAO.findByUserId(userId);
		ValidateUtil.isTrue(uInfo == null, userId+"用户已存在");
		
		Integer userType = userInfo.getUserType();		
		Integer isRealName = userInfo.getIsRealName();
		String identityNo = userInfo.getIdentityNo();		
		String realName = userInfo.getRealName();
		String mobile = userInfo.getMobile();
		Date createTime = userInfo.getCreateTime();
		String createUser = userInfo.getCreateUser();
		Date updateTime = userInfo.getUpdateTime();
		String updateUser = userInfo.getUpdateUser();
		String email = userInfo.getEmail();
		String remark = userInfo.getRemark();
		Integer grayState = userInfo.getGrayState();
				
		ValidateUtil.notNull(userType, "userType不能为空");		
		List<UserType> uType = UserType.listOf(userType);
		ValidateUtil.notNull(uType, "userType参数错误");
		
		if (isRealName != null && isRealName.intValue() == 1) {
			ValidateUtil.isNotBlank(identityNo, "实名认证用户identityNo不能为空");
			ValidateUtil.isNotBlank(realName, "实名认证用户realName不能为空");			
			ValidateUtil.isTrue(IDCardUtil.isIDCard(identityNo), "identityNo格式不正确");	
			
			userInfo.setIsRealName(1);
			userInfo.setGrade(ActLevel.LEVEL_V2.getCode());	
		}else {			
			userInfo.setIdentityNo(null);
			userInfo.setRealName(null);
			userInfo.setIsRealName(0);
			userInfo.setGrade(ActLevel.LEVEL_V1.getCode());
		}
		
		if (StringUtil.isNotBlank(mobile)) {
			ValidateUtil.isMobile(mobile, "mobile格式不正确");
		}
		
		if (createTime == null) {
			createTime = new Date();
		}
		
		if (updateTime == null) {
			updateTime = new Date();
		}
		
		if (StringUtil.isBlank(createUser)) {
			createUser = "admin";
		}
		
		if (StringUtil.isBlank(updateUser)) {
			updateUser = "admin";
		}
		
		if (StringUtil.isNotBlank(email)) {
			ValidateUtil.isEmail(email, "email格式不正确");
		}else {
			userInfo.setEmail("");
		}
		
		if (StringUtil.isBlank(remark)) {
			userInfo.setRemark("");
		}
		
		if (grayState == null) {
			grayState = 0;
		}
		
		userInfo.setCreateTime(createTime);
		userInfo.setCreateUser(createUser);
		userInfo.setUpdateTime(updateTime);
		userInfo.setUpdateUser(updateUser);
		userInfo.setUserStatus(1);
		userInfo.setIdentityType(IdCardType.IC_IDCARD.getCode());
		userInfo.setGrayState(grayState);
		
		//以下字段暂不使用
		userInfo.setIsEmailBinded(0);
		userInfo.setIsMobileBinded(0);
		userInfo.setPayPassword("");
		userInfo.setPaySalt("");				
		userInfo.setShortPayPassword("");
		userInfo.setAddress("");
		userInfo.setPhone("");
		
		
		userInfoMysqlDAO.save(userInfo);
		
		if (grayState == 1) {
			grayUser(userId);
		}
		
	}

	@Override
	public void realName(String userId, String identityNo, String realName,
			String mobile, String updateUser, String remark) {
		
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		UserInfoDO uInfo = userInfoMysqlDAO.findByUserId(userId);
		String originMobile = uInfo.getMobile();
		ValidateUtil.notNull(uInfo, userId+"用户不存在");
		
		Integer uStatus = uInfo.getUserStatus();
		if (uStatus != null && uStatus.intValue() == 0) {
			throw new BusinessException("用户已冻结");
		}
		
		Integer isRealName = uInfo.getIsRealName();
		if (isRealName != null && isRealName.intValue() == 1) {
			throw new BusinessException("用户已实名认证");
		}
		
		UserInfoDO newUserInfo = new UserInfoDO();
		
		ValidateUtil.isNotBlank(identityNo, "实名认证用户identityNo不能为空");
		ValidateUtil.isNotBlank(realName, "实名认证用户realName不能为空");			
		ValidateUtil.isTrue(IDCardUtil.isIDCard(identityNo), "identityNo格式不正确");
		
		if (StringUtil.isNotBlank(mobile)) {
			ValidateUtil.isMobile(mobile, "mobile格式不正确");
		}
		
		ValidateUtil.isNotBlank(updateUser, "updateUser不能为空");	
		
		newUserInfo.setIdentityNo(identityNo);
		newUserInfo.setRealName(realName);
		if(StringUtil.isBlank(originMobile)){
			newUserInfo.setMobile(mobile);
		}
		newUserInfo.setUpdateUser(updateUser);
		newUserInfo.setRemark(remark);
		newUserInfo.setGrade(ActLevel.LEVEL_V2.getCode());
		newUserInfo.setIsRealName(1);
		
		userInfoMysqlDAO.updateUserInfo(userId, newUserInfo);
	}

	@Override
	public UserInfoDO findUserInfo(String userId) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		UserInfoDO uInfo = userInfoMysqlDAO.findByUserId(userId);
		
		return uInfo;
	}
	
	@Override
	public void freeze(String userId, String updateUser, String remark) {
		
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		UserInfoDO uInfo = userInfoMysqlDAO.findByUserId(userId);
		ValidateUtil.notNull(uInfo, userId+"用户不存在");
		
		Integer uStatus = uInfo.getUserStatus();
		if (uStatus != null && uStatus.intValue() == 0) {
			LOG.warn(userId+"用户账户已被冻结");
			return ;
		}		
		ValidateUtil.isNotBlank(updateUser, "updateUser不能为空");	
		
		UserInfoDO newUserInfo = new UserInfoDO();		
		newUserInfo.setUserStatus(0);
		newUserInfo.setUpdateUser(updateUser);
		newUserInfo.setRemark(remark);
		
		userInfoMysqlDAO.updateUserInfo(userId, newUserInfo);
	}

	@Override
	public void unfreeze(String userId, String updateUser, String remark) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		UserInfoDO uInfo = userInfoMysqlDAO.findByUserId(userId);
		ValidateUtil.notNull(uInfo, userId+"用户不存在");
		
		Integer uStatus = uInfo.getUserStatus();
		if (uStatus != null && uStatus.intValue() == 1) {
			LOG.warn(userId+"用户账户未被冻结");
			return ;
		}		
		ValidateUtil.isNotBlank(updateUser, "updateUser不能为空");	
		
		UserInfoDO newUserInfo = new UserInfoDO();		
		newUserInfo.setUserStatus(1);
		newUserInfo.setUpdateUser(updateUser);
		newUserInfo.setRemark(remark);
		
		userInfoMysqlDAO.updateUserInfo(userId, newUserInfo);
	}

	@Override
	public void updateUserInfo(String userId, UserInfoDO newUserInfo) {		
		Integer grayState = newUserInfo.getGrayState();
		
		userInfoMysqlDAO.updateUserInfo(userId, newUserInfo);
		if (grayState != null && grayState.intValue() == 1) {
			grayUser(userId);
		}else if(grayState != null && grayState.intValue() == 0)  {
			rmGrayUser(userId);
		}
	}

	@Override
	public List<UserInfoDO> listUserInfo() {
		
		return null;
	}
	
	private void grayUser(String userId){
		
		userRedisDAO.addGrayUser(userId);
	}
	
	private void rmGrayUser(String userId){
		userRedisDAO.rmoveGrayUser(userId);
	}

}
